package com.yitongmao.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.bean.LoginHuancunBean;
import com.yitongmao.bean.OrderSuccessBean;
import com.yitongmao.bean.SuccessBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.cache.ACache;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.SignUtil;
import com.yitongmao.yitmfruit.R;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class IndentSuccessActivity extends AppCompatActivity {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.successPrice)
    TextView successPrice;
    @Bind(R.id.successWay)
    TextView successWay;
    @Bind(R.id.mUserZH)
    TextView mUserZH;
    @Bind(R.id.mKaiHuHang)
    TextView mKaiHuHang;
    @Bind(R.id.mLianHangHao)
    TextView mLianHangHao;
    @Bind(R.id.mHuiBianMa)
    TextView mHuiBianMa;
    @Bind(R.id.mUserBody)
    TextView mUserBody;
    @Bind(R.id.mGongsi)
    TextView mGongsi;
    private LoginHuancunBean loginHuancunBean;
    private Gson gson;
    private String loginid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerSion();
        setContentView(R.layout.activity_indent_success);
        ButterKnife.bind(this);
        setData();
    }

    private void setData() {
        gson = new Gson();
        //解析查找的缓存并判断
        String loginId = ACache.get(IndentSuccessActivity.this).getAsString("loginId");
        if (loginId != null) {
            loginHuancunBean = gson.fromJson(loginId, LoginHuancunBean.class);
            int loginId1 = loginHuancunBean.userInfo.loginId;
            loginid = String.valueOf(loginId1);

        }
        tvTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IndentSuccessActivity.this.finish();
            }
        });
        String submitsuccess = ACache.get(IndentSuccessActivity.this).getAsString("submitsuccess");
        OrderSuccessBean orderSuccessBean = gson.fromJson(submitsuccess, OrderSuccessBean.class);
        sendSuccess(orderSuccessBean.pi.phone, orderSuccessBean.pi.code, loginid);
    }

    //沉浸式状态栏
    public void immerSion() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
    }

    //交易成功
    public void sendSuccess(String phone, String code, String loginId) {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("phone", phone);
        params.put("code", code);
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("signLogin", SignUtil.signLogin(nonce, timestamp));
        params.put("loginId", loginId);
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.closeAnSuccessUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(Object result) {
                String string = (String) result;
                Log.e("Gson", "" + string);
                Gson gson = new Gson();
                SuccessBean successBean = gson.fromJson(string, SuccessBean.class);
                if (successBean.result.state.equals("1")) {
                    successPrice.setText(successBean.payInfo.totalPrice + "");
                    successWay.setText(successBean.payInfo.payModeName);
                    mUserBody.setText(successBean.comAccount.payInstruction);
                    mGongsi.setText(successBean.comAccount.companyName);
                    mUserZH.setText(successBean.comAccount.account);
                    mKaiHuHang.setText(String.valueOf(successBean.comAccount.openingBank));
                    mHuiBianMa.setText(successBean.payInfo.remittanceCode);
                }
            }

            @Override
            public void onFail(Object result) {
                System.out.print("失败");
            }
        });
    }


}
